﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BWcsasrsNetOutPut
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/21 14:52:23
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
using K8.MDCBB;
using KingerRobot.MexicoSailunMDC.Dao;

namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BWcsAsrsCrnAbnormalOutPut : IWcsAsrsCrnAbnormalOutPut
{
    private readonly static IWcsLocSku _WcsLocSku = new BWcsLocSku();
    private readonly static IRcsLocation _RcsLocation = new BRcsLocation();
    private readonly static ITask _Task = new BTask();
    private readonly static IWcsAsrsCrnOutPut _WcsAsrsCrnOutPut = new BWcsAsrsCrnOutPut();
    private readonly static IWcsNetAbnormal _WcsNetAbnormal = new BWcsNetAbnormal();
    public POWcsAsrsCrnAbnormalOutput GetAsrsCrnAbnormalOutPutByCrnId(long crnId)
    {
        var db = Db.Default;

        var wcsAsrsCrnOutput = db.WcsAsrsCrnAbnormalOutputs.Where(t => t.CrnId == crnId).FirstOrDefault();
        return wcsAsrsCrnOutput;
    }

    public POWcsAsrsCrnAbnormalOutput GetAsrsCrnAbnormalOutPutByLocId(long locId)
    {
        var db = Db.Default;
        var wcsAsrsCrnOutput = db.WcsAsrsCrnAbnormalOutputs.Where(t => t.OutputLoc1 == locId || t.OutputLoc2 == locId).FirstOrDefault();
        return wcsAsrsCrnOutput;
    }
    public List<POWcsAsrsCrnAbnormalOutput> GetWcsAsrsCrnAbnormalOutputs()
    {
        var db = Db.Default;
        var result = db.WcsAsrsCrnAbnormalOutputs
            .ToList();
        return result;
    }
    public long GetTargetAbnormalSingleLoc(long? skuId, CrnForkInfo fork, int outPutType)
    {
        var db = Db.Default;
        long result = 0;
        var locs = _RcsLocation.GetHasSkuLocationsByType((int)EnumLocType.Location);
        var wcsSkuLoc = _WcsLocSku.GetWcsLocSkusBySkuId(skuId);
        var hasLocs = locs.Join(wcsSkuLoc, f => f.Id, s => s.LocId, (f, s) => new
        {
            LocId = f.Id
        }).ToList();
        //立库异常位置
        var abnormalOutPut = GetAsrsCrnAbnormalOutPutByCrnId(fork.CrnId.GetValueOrDefault());

        //出库放胎位GetWcsAsrsCrnAbnormalOutputsByCrnId
        var crnOutPut = _WcsAsrsCrnOutPut.GetWcsAsrsCrnAbnormalOutputsByCrnId(fork.CrnId.GetValueOrDefault()).FirstOrDefault();
        //异常口
        var abnormal = _WcsNetAbnormal.GetWcsNetAbnormalByStartLoc(fork.CrnId.GetValueOrDefault());
        if (abnormal is null)
        {
            return 0;
        }
        //线体异常口
        var abnormalTargetLoc = abnormal.TargetLoc.GetValueOrDefault();

        //如果已经在库里 则生成异常口任务
        if (hasLocs is not null && hasLocs.Any())
        {
            // 立库异常口
            if (outPutType == 1)
            {
                result = abnormalOutPut?.OutputLoc1 ?? 0;
            }
            else
            {
                result = abnormalOutPut?.OutputLoc2 ?? 0;
            }
        }
        //如果任务是去到异常口的任务 则目标位返回异常口
        var task = _Task.GetTaskBySkuId(skuId ?? 0);
        if (task is not null)
        {
            var taskTarget = task.TargetLoc;
            if (taskTarget == abnormalOutPut?.OutputLoc1 || taskTarget == abnormalOutPut?.OutputLoc2)
            {
                // 立库异常口
                if (outPutType == 1)
                {
                    result = abnormalOutPut?.OutputLoc1 ?? 0;
                }
                else
                {
                    result = abnormalOutPut?.OutputLoc2 ?? 0;
                }
            }
            //如果任务是去线体异常口的 先生成去堆垛机出库口的任务
            if (taskTarget == abnormalTargetLoc) 
            {
                if (outPutType == 1)
                {
                    result = crnOutPut?.OutputLoc1 ?? 0;
                }
                else
                {
                    result = crnOutPut?.OutputLoc2 ?? 0;
                }
            }

        }
        return result;
    }

}
